﻿$(function () {
    $("#searchBox").autocomplete(
    {
        source: function (request, response) {
            $.ajax(
            {
                url: "/search/findkeywords", type: "POST", dataType: "json",
                data: { searchText: request.term, maxResults: 10 },
                success: function (data) {
                    response($.map(data, function (item) {
                        return { label: item, value: item, id: item }
                            }
                    ))
                }
            })
        }
    });
});

/* Dutch (UTF-8) initialisation for the jQuery UI date picker plugin. */
/* Written by Mathias Bynens <http://mathiasbynens.be/> */
jQuery(function ($) {
    $.datepicker.regional['nl'] = {
        closeText: 'Sluiten',
        prevText: '←',
        nextText: '→',
        currentText: 'Vandaag',
        monthNames: ['Januari', 'Februari', 'Maart', 'April', 'Mei', 'Juni',
            'Juli', 'Augustus', 'September', 'Oktober', 'November', 'December'],
        monthNamesShort: ['jan', 'feb', 'maa', 'apr', 'mei', 'jun',
            'jul', 'aug', 'sep', 'okt', 'nov', 'dec'],
        dayNames: ['Zondag', 'Maandag', 'Dinsdag', 'Woensdag', 'Donderdag', 'Vrijdag', 'Zaterdag'],
        dayNamesShort: ['zon', 'maa', 'din', 'woe', 'don', 'vri', 'zat'],
        dayNamesMin: ['zo', 'ma', 'di', 'wo', 'do', 'vr', 'za'],
        weekHeader: 'Wk',
        dateFormat: 'dd-mm-yy',
        firstDay: 1,
        isRTL: false,
        showMonthAfterYear: false,
        yearSuffix: ''
    };
    $.datepicker.setDefaults($.datepicker.regional['nl']);
});

$(document).ready(function () {


    // ~~~ Auto postback on OrderBy select change
    if ($('#OrderBy').length === 1 && $('.searchbutton').length === 1) {
        $('#OrderBy').on('change', function () {
            $('.searchbutton').trigger('click');
        });
    }

    // ~~~ Toggle Filters
    function initToggle1() {
        if (0 < $('#customMobileFacetToggle').length && 0 < $('#facetContainer').length) {

            var $t = $('#customMobileFacetToggle');
            var $f = $('#facetContainer');
            var o = 'opened';

            function openF() {
                $t.addClass(o);
                $t.text('Verberg filters');
                $f.slideDown('fast');
            }

            function closeF() {
                $t.removeClass(o);
                $t.text('Toon filters');
                $f.slideUp('fast');
            }

            $t.on('click', function (e) {
                e.preventDefault();

                if (!$t.hasClass(o)) {
                    openF();
                } else {
                    closeF();
                }
            });
        }
    }initToggle1();

    // ~~~ Toggle Weergaveopties
    function initToggle2() {
        if (0 < $('#customMobileDisplayToggle').length && 0 < $('#customDisplayOptions').length) {

            var $t = $('#customMobileDisplayToggle');
            var $f = $('#customDisplayOptions');
            var o = 'opened';

            function openF() {
                $t.addClass(o);
                $t.text('Verberg weergaveopties');
                $f.slideDown('fast');
            }

            function closeF() {
                $t.removeClass(o);
                $t.text('Toon weergaveopties');
                $f.slideUp('fast');
            }

            $t.on('click', function (e) {
                e.preventDefault();

                if (!$t.hasClass(o)) {
                    openF();
                } else {
                    closeF();
                }
            });
        }
    }initToggle2();

    
    

    var selected = null;

    var blok5open = false;

    $('#fromDate').datepicker({ beforeShowDay: function (date) {
        if (selected != null && date.getTime() > selected.getTime() &&
                    (date.getTime() - selected.getTime()) < 12 * 24 * 3600 * 1000) {
            return [true, "highlighted"];
        }
        return [true, ""];
    }
    });

    $('#toDate').datepicker({ beforeShowDay: function (date) {
        if (selected != null && date.getTime() > selected.getTime() &&
                    (date.getTime() - selected.getTime()) < 12 * 24 * 3600 * 1000) {
            return [true, "highlighted"];
        }
        return [true, ""];
    }
    });

    var timeout = 200;

    $(".link1").bind('click', function (e) {
        e.preventDefault();
        setTimeout(function () {
            //$(".link1").parent().css('display', 'none');
            $("#block1").css('height', $('#moreBlock1').css('height'));
            $("#block1").css('border', '1px solid #000000');
        }, timeout);
        return false;
    });

    $("#block1").bind('focusout', function () {
        $("#block1").css('height', '0px');
        $("#block1").css('border', '0px solid #000000');
        $(".link1").parent().css('display', 'inline');
    });

    $(".link2").bind('click', function (e) {
        e.preventDefault();
        setTimeout(function () {
            //$(".link2").parent().css('display', 'none');
            $("#block2").css('height', $('#moreBlock2').css('height'));
            $("#block2").css('border', '1px solid #000000');
        }, timeout);
        return false;
    });

    $("#block2").bind('focusout', function () {
        $("#block2").css('height', '0px');
        $("#block2").css('border', '0px solid #000000');
        $(".link2").parent().css('display', 'inline');
    });

    $(".link3").bind('click', function (e) {
        e.preventDefault();
        setTimeout(function () {
            //$(".link3").parent().css('display', 'none');
            $("#block3").css('height', $('#moreBlock3').css('height'));
            $("#block3").css('border', '1px solid #000000');
        }, timeout);
        return false;
    });

    $("#block3").bind('focusout', function () {
        $("#block3").css('height', '0px');
        $("#block3").css('border', '0px solid #000000');
        $(".link3").parent().css('display', 'inline');
    });

    $(".link4").bind('click', function (e) {
        e.preventDefault();
        setTimeout(function () {
           // $(".link4").parent().css('display', 'none');
            $("#block4").css('height', $('#moreBlock4').css('height'));
            $("#block4").css('border', '1px solid #000000');
        }, timeout);
        return false;
    });

    $("#block4").bind('focusout', function () {
        $("#block4").css('height', '0px');
        $("#block4").css('border', '0px solid #000000');
        $(".link4").parent().css('display', 'inline');
    });

    $(".link5").bind('click', function (e) {
        e.preventDefault();
        setTimeout(function () {
            //$(".link5").parent().css('display', 'none');
            if ($('#moreBlock5').css('height') < 300) {
                $("#block5").css('height', $('#moreBlock5').css('height'));
            }
            else {
                $("#block5").css('height', 300);
            }
            $("#block5").css('border', '1px solid #000000');
        }, timeout);
        return false;
    });
    $("#block5").bind('focusout', function () {
        $("#block5").css('height', '0px');
        $("#block5").css('border', '0px solid #000000');
        $(".link5").parent().css('display', 'inline');
    });

    $(".link6").bind('click', function (e) {
        e.preventDefault();
        setTimeout(function () {
            //$(".link6").parent().css('display', 'none');
            $("#block6").css('height', $('#moreBlock6').css('height'));
            $("#block6").css('border', '1px solid #000000');
        }, timeout);
        return false;
    });

    $("#block6").bind('focusout', function () {
        $("#block6").css('height', '0px');
        $("#block6").css('border', '0px solid #000000');
        $(".link6").parent().css('display', 'inline');
    });

    $(".link7").bind('click', function (e) {
        e.preventDefault();
        setTimeout(function () {
            //$(".link7").parent().css('display', 'none');
            if ($('#moreBlock7').css('height') < 300) {
                $("#block7").css('height', $('#moreBlock7').css('height'));
            }
            else {
                $("#block7").css('height', 300);
            }
            $("#block7").css('border', '1px solid #000000');
        }, timeout);
        return false;
    });

    $("#block7").bind('focusout', function () {
        $("#block7").css('height', '0px');
        $("#block7").css('border', '0px solid #000000');
        $(".link7").parent().css('display', 'inline');
    });

    // Click outside to hide.
    $(document).mouseup(function (e) {
        var container = $('#block1, #block2, #block3, #block4, #block5, #block6, #block7');

        try {
            if (!container.is(e.target) && container.has(e.target).length === 0) {
                container.trigger('focusout');
            }
        } catch (err) {
            // nothing.
        }
    });


    /* test does not work? 
        for (i = 1; i <= 7; i++) {
        alert('i=' + i)
        if (i == 5) {
            alert('i=5 ...' + i)
            $(".link" + i).bind('mouseenter focusin', function () {
                $(".link" + i).parent().css('display', 'none');
                if ($('#moreBlock' + i).css('height') < 300) {
                    $("#block" + i).css('height', $('#moreBlock' + i).css('height'));
                }
                else {
                    $("#block" + i).css('height', 300);
                }
                $("#block" + i).css('border', '1px solid #000000');
                return false;
            });
            $("#block" + i).bind('mouseleave focusout', function () {
                $("#querytext").val('leave');
                $("#block" + i).css('height', '0px');
                $("#block" + i).css('border', '0px solid #000000');
                $(".link" + i).parent().css('display', 'inline');
            });
        } else {
            $(".link" + i).bind('mouseenter focusin', function () {
                $(".link" + i).parent().css('display', 'none');
                $("#block" + i).css('height', $('#moreBlock' + i).css('height'));
                $("#block" + i).css('border', '1px solid #000000');
                return false;
            });

            $("#block" + i).mouseleave(function () {
                $("#block" + i).css('height', '0px');
                $("#block" + i).css('border', '0px solid #000000');
                $(".link" + i).parent().css('display', 'inline');
            });
        }
    }
    */

}); 



